package com.onstar.cn.weight;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.onstar.cn.common.cache.ImageCache;

public class UnreadMarkFrameLayout extends FrameLayout {
    private Context context;
    private boolean showMark;
    private TypedArray typedArray;
    private float marginRight;
    private float marginTop;
    private ImageView redMark;

    public UnreadMarkFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        typedArray = context.obtainStyledAttributes(attrs, R.styleable.UnreadMarkParam);
    }

    public UnreadMarkFrameLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public UnreadMarkFrameLayout(Context context) {
        this(context, null);
    }

    private void initView() {
        marginRight = typedArray.getDimension(R.styleable.UnreadMarkParam_margin_right, 0);
        marginTop = typedArray.getDimension(R.styleable.UnreadMarkParam_margin_top, 0);
        redMark = new CustomImageView(context);
        redMark.setImageDrawable(ImageCache.getInstance(context).getCache(R.drawable.unread_mark, true));
        LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        layoutParams.topMargin = (int) marginTop;
        layoutParams.rightMargin = (int) marginRight;
        layoutParams.gravity = Gravity.RIGHT;
        redMark.setLayoutParams(layoutParams);
    }

    public void showMark(boolean showMark) {
        this.showMark = showMark;
        if (showMark) {
            if (redMark == null) {
                initView();
                addView(redMark);
            }
        } else {
            if (redMark != null) {
                removeView(redMark);
                redMark = null;
            }
        }
    }

    public boolean isMarkShowing() {
        return showMark;
    }
}
